package com.example.home.hospital.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.hospital.adapter.RegistrationAdapter;
import com.example.home.hospital.bean.AllCardBean;
import com.example.home.hospital.bean.RegistrationBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class RegistrationActivity extends AppCompatActivity {

    private ImageView back;
    private RecyclerView rcv_list;
    private AllCardBean.RowsBean rowsBean1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        initView();
        Intent intent = getIntent();
        String rowsBeanJson = intent.getStringExtra("rowsBeanJson");
        Gson gson = new Gson();
        rowsBean1 = gson.fromJson(rowsBeanJson, AllCardBean.RowsBean.class);

        // 调用获取数据的方法
        getListData();
    }

    private void getListData() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/category/list",
                OKHttpTool.GET,
                RegistrationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RegistrationBean registrationBean = (RegistrationBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindListData(registrationBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 处理请求失败的情况
                    }
                }
        );
    }


    private void bindListData(RegistrationBean registrationBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        RegistrationAdapter hospitalListAdapter = new RegistrationAdapter(RegistrationActivity.this, registrationBean.getRows(), rowsBean1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
        recyclerView.setAdapter(hospitalListAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }


    private void initView() {
        rcv_list = findViewById(R.id.rcv_list);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegistrationActivity.this.finish();
            }
        });
    }
}